package com.chilli.cosima.action.providermanager;

import java.io.File;
import java.util.Date;

import org.apache.struts2.ServletActionContext;

import com.chilli.cosima.action.BaseSupport;
import com.chilli.cosima.common.BussinessException;
import com.chilli.cosima.data.bean.Provider;
import com.chilli.cosima.service.ProviderService;

public class ProviderAddAction extends BaseSupport
{

	private static final long serialVersionUID = -1358098648224700190L;

	private ProviderService providerService;
	private Provider provider;
	private File myFile;
	private String myFileFileName;

	public String jumpPage()
	{
		return JUMP_PAGE;
	}

	public String execute()
	{
		try
		{
			if (null != myFile)
			{

				String imageFileName = new Date().getTime()
						+ getExtention(myFileFileName);

				File imageFile = new File(ServletActionContext
						.getServletContext().getRealPath(
								"/upload/images/provider")
						+ "/" + imageFileName);

				copy(myFile, imageFile);
				provider.setPicture(imageFileName);
			}

			providerService.createProvider(provider);
			return SUCCESS;
		} catch (BussinessException e)
		{
			logger.error("error", e);
		} catch (Exception e)
		{

		}
		return JUMP_PAGE;
	}

	public ProviderService getProviderService()
	{
		return providerService;
	}

	public void setProviderService(ProviderService providerService)
	{
		this.providerService = providerService;
	}

	public Provider getProvider()
	{
		return provider;
	}

	public void setProvider(Provider provider)
	{
		this.provider = provider;
	}

	public File getMyFile()
	{
		return myFile;
	}

	public void setMyFile(File myFile)
	{
		this.myFile = myFile;
	}

	public String getMyFileFileName()
	{
		return myFileFileName;
	}

	public void setMyFileFileName(String myFileFileName)
	{
		this.myFileFileName = myFileFileName;
	}

}
